package me.arvin.qa.jmeter.plugins.dependscontroller;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.UUID;

import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.property.AbstractProperty;

/**
 * 依赖关系
 *
 * @author wenke.dong
 *
 */
class DependsRelationProperty extends AbstractProperty implements Serializable {

	private static final long serialVersionUID = 8792071323467986048L;

	private String id;

	/**
	 * 依赖项
	 */
	private LinkedHashSet<String> dependsOn;

	/**
	 * 被依赖项
	 */
	private LinkedHashSet<String> BeDependsOn;

	static {
		DependsManageTreeLisener.addToTree();
	}

	public DependsRelationProperty() {
		this(null);
	}

	/**
	 * 自定义组件id
	 *
	 * @param id
	 */
	public DependsRelationProperty(String id) {
		super(DependsController.DependsRelationList);

		if (id != null) {
			this.id = id;
		} else {
			this.id = UUID.randomUUID().toString();
		}
		dependsOn = new LinkedHashSet<String>();
		BeDependsOn = new LinkedHashSet<String>();
	}

	public void setDepends(LinkedHashSet<String> dependsOn) {
		this.dependsOn = dependsOn;
	}

	public void setBeDepends(LinkedHashSet<String> BeDependsOn) {
		this.BeDependsOn = BeDependsOn;
	}

	public LinkedHashSet<String> getDepends() {
		return dependsOn;
	}

	public LinkedHashSet<String> getBeDepends() {
		return BeDependsOn;
	}

	public void addDepends(String... beDependsIds) {
		for (String beDependsId : beDependsIds) {
			dependsOn.add(beDependsId);
		}
	}

	public void addBeDepends(String beDependsId) {
		BeDependsOn.add(beDependsId);
	}

	public String setId(String id) {
		return this.id = id;
	}

	public String getId() {
		return id;
	}

	@Override
	public String toString() {
		return "name=" + getName() + "; id= " + id + "; dependsOn: " + dependsOn + "; BeDependsOn: " + BeDependsOn;
	}

	@SuppressWarnings("unchecked")
	@Override
	public DependsRelationProperty clone() {
		String id = null;

		boolean cloneThis = isRunningVersion() || clone4startTest();
		if (cloneThis) {
			id = getId();
		}

		DependsRelationProperty drp = new DependsRelationProperty(id);

		drp.setDepends((LinkedHashSet<String>) getDepends().clone());
		if (cloneThis) {
			drp.setBeDepends((LinkedHashSet<String>) getBeDepends().clone());
		}
		return drp;
	}

	private static boolean clone4startTest() {
		for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
			// org.apache.jmeter.gui.action.Duplicate.doAction
			// org.apache.jmeter.gui.action.Start.doAction
			// org.apache.jmeter.gui.action.Copy.doAction
			// org.apache.jmeter.gui.action.CheckDirty.doAction
			if (("org.apache.jmeter.gui.action.Start".equals(ste.getClassName())
					|| "org.apache.jmeter.gui.action.CheckDirty".equals(ste.getClassName()))
					&& "doAction".equals(ste.getMethodName())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void recoverRunningVersion(TestElement owner) {}

	@Override
	public String getStringValue() {
		return toString();
	}

	@Override
	public Object getObjectValue() {
		return null;
	}

	@Override
	public void setObjectValue(Object value) {

	}
}